/*****************************************************************************
*Purpose: Estimate relationship between sample linkage and treatment
******************************************************************************/ 

global fe "intakeyear yrbirth  mitrct80"
global notractfe "intakeyear yrbirth  "
global controls "female head_female headage married auto license work emp inc chicago ph i.bed"


capture frame drop attritnonresp
frame create attritnonresp str35 outcome str35 group  str35 spec beta se N piks dv1treatpiks dv1controlpiks dv0treatpiks dv0controlpiks

foreach aout in hascensus2010 hasage26tract hasage28tract has2007tract has2010tract has2013tract has2016tract has2019tract {


	* Base
	reghdfe `aout' generalarea $controls if  estsample==1, absorb( $fe ) cluster(hhid)
	unique pik if e(sample)==1
	
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1 & `aout'==1
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & `aout'==1
	local y1controlpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1 & `aout'==0
	local y0treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & `aout'==0
	local y0controlpik `=r(sum)'
	
	frame post attritnonresp  ("`aout'") ("kids") ("base") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')

	sum `aout' if e(sample)==1 & generalarea==0
	local cmean `=r(mean)'
	local csd `=r(sd)'
	local N `=r(N)'
	
	local allpik `=r(N)'
	unique pik if e(sample)==1 & generalarea==1 & `aout'==1
	local y1treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & `aout'==1
	local y1controlpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==1 & `aout'==0
	local y0treatpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & `aout'==0
	local y0controlpik `=r(sum)'

	frame post attritnonresp  ("`aout'") ("kids") ("meansd") (`cmean') (`csd') (`N') (`allpik') (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')

	
}

capture frame drop blankframe
frame create blankframe
frame change blankframe

use allmemsamp.dta, clear


reghdfe haspik generalarea if estsample==1, absorb($fe) cluster(hhid)

	unique uniqueid if e(sample)==1
	
	local allpik `=r(sum)'
	unique uniqueid if e(sample)==1 & generalarea==1 & haspik==1
	local y1treatpik `=r(sum)'
	unique uniqueid if e(sample)==1 & generalarea==0 & haspik==1
	local y1controlpik `=r(sum)'
	unique uniqueid if e(sample)==1 & generalarea==1 & haspik==0
	local y0treatpik `=r(sum)'
	unique uniqueid if e(sample)==1 & generalarea==0 & haspik==0
	local y0controlpik `=r(sum)'
	
	
frame post attritnonresp  ("haspik") ("all") ("base") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')

	sum haspik  if e(sample)==1 & generalarea==0
	local cmean `=r(mean)'
	local csd `=r(sd)'
	local N `=r(N)'
	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & haspik==1
	local y1controlpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & haspik==0
	local y0controlpik `=r(sum)'

frame post attritnonresp  ("haspik") ("all") ("meansd") (`cmean') (`csd') (`N')  (`allpik') (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')


reghdfe haspik generalarea if estsample==1 & prtcpnt=="M", absorb($fe) cluster(hhid)

	unique uniqueid if e(sample)==1
	
	local allpik `=r(sum)'
	unique uniqueid if e(sample)==1 & generalarea==1 & haspik==1
	local y1treatpik `=r(sum)'
	unique uniqueid if e(sample)==1 & generalarea==0 & haspik==1
	local y1controlpik `=r(sum)'
	unique uniqueid if e(sample)==1 & generalarea==1 & haspik==0
	local y0treatpik `=r(sum)'
	unique uniqueid if e(sample)==1 & generalarea==0 & haspik==0
	local y0controlpik `=r(sum)'
	
	
frame post attritnonresp  ("haspik") ("kids") ("base") (_b[generalarea]) (_se[generalarea]) (e(N)) (`allpik') (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')

	sum haspik  if e(sample)==1 & generalarea==0
	local cmean `=r(mean)'
	local csd `=r(sd)'
	local N `=r(N)'

	local allpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & haspik==1
	local y1controlpik `=r(sum)'
	unique pik if e(sample)==1 & generalarea==0 & haspik==0
	local y0controlpik `=r(sum)'

frame post attritnonresp  ("haspik") ("kids") ("meansd") (`cmean') (`csd') (`N') (`allpik') (`y1treatpik') (`y1controlpik') (`y0treatpik') (`y0controlpik')

frame change default
frame attritnonresp: save "${output}/attritnonresp_results.dta", replace
frame attritnonresp: export delimited using "${output}/attritnonresp_results.csv", replace
